//
//  SettViewController.m
//  YIChuSeller
//
//  Created by xsj on 2018/11/22.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXSettViewController.h"
#import "GXChangePhoneController.h"
#import "GXChangePassWordController.h"
#import "GXNewsWarnController.h"
@interface GXSettViewController ()
@property (weak, nonatomic) IBOutlet UILabel *phoneNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;
@end

@implementation GXSettViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"设置";
    [self setUpValues];
}
- (void)setUpValues
{
    NSString * phoneNumberStr = [[NSUserDefaults standardUserDefaults]objectForKey:@"phoneNumber"];
    self.phoneNumberLabel.text =[NSString stringWithFormat:@"%@",phoneNumberStr];
    CGFloat size = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject];
    self.cacheLabel.text = [NSString stringWithFormat:@"%.2fM",size];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    self.versionLabel.text = [NSString stringWithFormat:@"V%@",app_build];
}
- (IBAction)pressChangePhone:(UIButton *)sender {
    GXChangePhoneController * phoneVC = [[GXChangePhoneController alloc]init];
    [self.navigationController pushViewController:phoneVC animated:YES];
}
- (IBAction)pressChangePassword:(UIButton *)sender {
    GXChangePassWordController * passVC = [[GXChangePassWordController alloc]init];
    [self.navigationController pushViewController:passVC animated:YES];
}
- (IBAction)pressNewsWarn:(UIButton *)sender {
    GXNewsWarnController * newsVC = [[GXNewsWarnController alloc]init];
    [self.navigationController pushViewController:newsVC animated:YES];
}
- (IBAction)pressCleanCache:(UIButton *)sender {
    [self cleanCaches];
    self.cacheLabel.text = @"0M";
    
}
- (IBAction)pressAbout:(UIButton *)sender {
}
- (IBAction)pressVersionUpdate:(UIButton *)sender {
    
}
// 计算目录大小
- (CGFloat)folderSizeAtPath:(NSString *)path{
    NSFileManager *manager = [NSFileManager defaultManager];
    CGFloat size = 0;
    if ([manager fileExistsAtPath:path]) {
        NSArray *childrenFile = [manager subpathsAtPath:path];
        for (NSString *fileName in childrenFile) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            size += [manager attributesOfItemAtPath:absolutePath error:nil].fileSize;
        }
        return size / 1024.0 / 1024.0;
    }
    return 0;
}
// 根据路径删除文件
- (void)cleanCaches{
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
    for (NSString *fileName in files) {
        NSError *error;
        NSString *path = [cachPath stringByAppendingPathComponent:fileName];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            if (error){
                MyLog(@"%@",[NSString stringWithFormat:@"%@这个路径的文件夹删除失败了",fileName]);
            }else{
                [SVProgressHUD showSuccessWithStatus:@"清理完成"];
                }
            }
    }
}

@end
